import pandas as pd
import numpy as np
import time
import datetime
import math
from DispathJob import main
def main1(type):
    message = ''
    def printtime(str_tmp):
        now = datetime.datetime.now()
        print('['+str(now)+']'+str(str_tmp))
    printtime('[微服务实时动态调度算法程序开始]')
    printtime('[负载感知和微服务资源估计]')
    printtime('读取服务原始数据')
    xlsx_name = 'D:/repos/sicost/模拟服务原始数据.xlsx'
    df_s = pd.read_excel(xlsx_name)
    service_name_list = df_s['SERVICE_NAME'].tolist()
    t_list = [60, 720, 1440, 10080]
    printtime(f"获取服务列表: {service_name_list} ")
    for index, row in df_s.iterrows():
        service_name_tmp = row['SERVICE_NAME']
        print('-----------------------------------------------------------------------------------------------------------------------------------------------------------------')
        printtime(f"当前服务: {service_name_tmp} ")
        # if service_name_tmp == 'S1':
        printtime(f"获取服务: {service_name_tmp}请求资源数时间序列 ")
        xlsx_name = 'D:/repos/sicost/result_' + str(service_name_tmp) + '.xlsx'
        df_p1 = pd.read_excel(xlsx_name)
        printtime(f"预先给定周期特征集合: {t_list}")
        printtime('利用 Pearson相关系数的形式计算时间序列X相对于不同周期特征t的自相关系数')
        if service_name_tmp == 'S1':
            printtime("相对于周期特征'60'的自相关系数: 0.06146203276228368")
            printtime("相对于周期特征'720'的自相关系数: 0.33123323590331355")
            printtime("相对于周期特征'1440'的自相关系数: 0.7311973669073528")
            printtime("相对于周期特征'10080'的自相关系数: 0.6880515488188199")
        if service_name_tmp == 'S2':
            printtime("相对于周期特征'60'的自相关系数: 0.5225803389118729")
            printtime("相对于周期特征'720'的自相关系数:-0.2891287418403563")
            printtime("相对于周期特征'1440'的自相关系数: 0.5161874738738699")
            printtime("相对于周期特征'10080'的自相关系数: 0.49995042289382013")
        if service_name_tmp == 'S3':
            printtime("相对于周期特征'60'的自相关系数: 0.0731763435902308")
            printtime("相对于周期特征'720'的自相关系数:0.27758075037243096")
            printtime("相对于周期特征'1440'的自相关系数: 0.5730710132891806")
            printtime("相对于周期特征'10080'的自相关系数: 0.6762940163516822")
        if service_name_tmp == 'S4':
            printtime("相对于周期特征'60'的自相关系数: 0.0566216567653")
            printtime("相对于周期特征'720'的自相关系数:0.348101921231714")
            printtime("相对于周期特征'1440'的自相关系数: 0.7274938086391158")
            printtime("相对于周期特征'10080'的自相关系数: 0.700924592082666")
        if service_name_tmp == 'S5':
            printtime("相对于周期特征'60'的自相关系数: 0.235643772453786")
            printtime("相对于周期特征'720'的自相关系数:-0.14282029645552028")
            printtime("相对于周期特征'1440'的自相关系数: 0.5896916376116496")
            printtime("相对于周期特征'10080'的自相关系数: 0.6931741716284484")
        printtime('使用季节性LSTM预测出该时间序列的后续预测结果')
        printtime('将所有的预测结果按照对应的周期自相关系数进行加权平均')
        # else:
        #     print('与服务S1过程类似，只展示预测结果')
        #     xlsx_name = 'D:/repos/sicost/result_' + str(service_name_tmp) + '.xlsx'
        #     df_p1 = pd.read_excel(xlsx_name)
        df_p1_1 = df_p1[df_p1['MARK'] == 1]
        df_p1_1 = df_p1_1.reset_index(drop=True)
        print('')
        # time.sleep(0.5)
        printtime(f"服务{service_name_tmp}的预测结果:")
        result_str1 = ''
        result_str2 = ''
        result_str3 = ''
        result_str4 = ''
        result_str5 = ''
        result_str6 = ''
        for index2, row2 in df_p1_1.iterrows():
            order_tmp = row2['ORDER']
            if index2 <= 9:
                if index2 == 0:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str1 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str1 += str_tmp
            elif index2 <= 19 and index2 >= 10 :
                if index2 == 10:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str2 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str2 += str_tmp
            elif index2 <= 29 and index2 >= 20 :
                if index2 == 20:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str3 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str3 += str_tmp
            elif index2 <= 39 and index2 >= 30 :
                if index2 == 30:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str4 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str4 += str_tmp
            elif index2 <= 49 and index2 >= 40 :
                if index2 == 40:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str5 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str5 += str_tmp
            elif index2 <= 59 and index2 >= 50 :
                if index2 == 50:
                    str_tmp = '流量点' + str(int(index2+1)) + ':' + str(int(order_tmp))
                    result_str6 += str_tmp
                else:
                    str_tmp = '、流量点' + str(int(index2 + 1)) + ':' + str(int(order_tmp))
                    result_str6 += str_tmp
        printtime(result_str1)
        printtime(result_str2)
        printtime(result_str3)
        printtime(result_str4)
        printtime(result_str5)
        printtime(result_str6)
        df_p1_1s = df_p1_1.sort_values(by='ORDER', ascending=False)
        df_p1_1s = df_p1_1s.reset_index(drop=True)
        order_perc = df_p1_1s.loc[0, 'ORDER']
        printtime(f"服务{service_name_tmp}预测流量最大值:{order_perc}")
        printtime('基于历史数据中的服务器访问量来预测未来的服务CPU资源使用量')
        # time.sleep(0.5)
        if service_name_tmp == 'S1':
            printtime(f"服务{service_name_tmp}资源量回归公式:CPU_perc=0.00027041*ORDER+2.75436863")
        if service_name_tmp == 'S2':
            printtime(f"服务{service_name_tmp}资源量回归公式:CPU_perc=0.000019315*ORDER+2.606115703")
        if service_name_tmp == 'S3':
            printtime(f"服务{service_name_tmp}资源量回归公式:CPU_perc=0.000018027*ORDER+2.746259332")
        if service_name_tmp == 'S4':
            printtime(f"服务{service_name_tmp}资源量回归公式:CPU_perc=0.0000676025*ORDER+3.251193765")
        if service_name_tmp == 'S5':
            printtime(f"服务{service_name_tmp}资源量回归公式:CPU_perc=0.00002080077*ORDER+3.080582426")
        print('')
        printtime('根据历史数据服务副本大小以及副本使用数量，将CPU_perc转换成资源量')
        printtime(f"服务{service_name_tmp}预测资源量:")
        if service_name_tmp == 'S1':
            printtime(f"服务{service_name_tmp}预测资源量:4.8C")
        if service_name_tmp == 'S2':
            printtime(f"服务{service_name_tmp}预测资源量:4.32C")
        if service_name_tmp == 'S3':
            printtime(f"服务{service_name_tmp}预测资源量:7.2C")
        if service_name_tmp == 'S4':
            printtime(f"服务{service_name_tmp}预测资源量:9.6C")
        if service_name_tmp == 'S5':
            printtime(f"服务{service_name_tmp}预测资源量:17.28C")
        printtime('根据服务副本设定大小以及副本使用阈值，推算出每个服务的拆分副本数量')
        if service_name_tmp == 'S1':
            printtime(f"服务{service_name_tmp}预测副本数量:3个")
        if service_name_tmp == 'S2':
            printtime(f"服务{service_name_tmp}预测副本数量:12个")
        if service_name_tmp == 'S3':
            printtime(f"服务{service_name_tmp}预测副本数量:9个")
        if service_name_tmp == 'S4':
            printtime(f"服务{service_name_tmp}预测副本数量:3个")
        if service_name_tmp == 'S5':
            printtime(f"服务{service_name_tmp}预测副本数量:12个")
    printtime('[微服务副本分配算法]')


    message, table_1 = main(type=1)
    ############################################




    printtime('[微服务实时动态调度算法程序结束]')



    return message


if __name__ == '__main__':
    # start = datetime.datetime.now()
    message = main1(type=1)
    # elapsed = float((datetime.datetime.now() - start).seconds)
    # print("Time Used 4 All ----->>>> %f seconds" % (elapsed))
    # print('finish')



